package com.flower.enjoyment.flower.config;

import com.flower.enjoyment.flower.base.WeChatConfig;
import com.flower.enjoyment.flower.utils.IpAddress;
import com.flower.enjoyment.flower.utils.RandomUtil;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;

/**
 * @date: 2018/11/28 16:45
 * @author: YINLELE
 * @description: 微信请求参数
 */
@Data
public class WxParam {
    /*微信分配的小程序ID*/
    private String appid;

    /*微信支付分配的商户号*/
    private String mch_id;

    /*随机字符串，长度要求在32位以内*/
    private String nonce_str;

    /*签名*/
    private String sign;

    /*商品描述*/
    private String body;

    /*商户订单号*/
    private String out_trade_no;

    /*标价金额 默认分*/
    private Integer total_fee;

    /*终端IP*/
    private String spbill_create_ip;

    /*通知地址*/
    private String notify_url;

    /*交易类型*/
    private String trade_type;

    /*用户的openid*/
    private String openid;



    /*组装微信支付请求的参数*/
    public void setWxParam(WxParam wxParam, String No,String body,Double amount,
                           HttpServletRequest request,
                           String  notify_url,String openid)
            throws UnsupportedEncodingException {
        wxParam.setAppid(WeChatConfig.WX_LOGIN_APPID);
        wxParam.setMch_id(WeChatConfig.MCH_ID);
        wxParam.setSpbill_create_ip(IpAddress.getIpAddress(request));
        wxParam.setTrade_type("JSAPI");
        wxParam.setNonce_str(RandomUtil.getUuid());
        wxParam.setOut_trade_no(No);
        wxParam.setBody(body);
        wxParam.setOpenid(openid);
        DecimalFormat df = new DecimalFormat("###################.###########");
        wxParam.setTotal_fee(Integer.valueOf(String.valueOf(df.format(amount*100))));
        //wxParam.setTotal_fee(1);
        wxParam.setNotify_url(notify_url);
        wxParam.setOpenid(openid);
    }


}
